package com.edu.servlet;

import com.edu.bean.News;
import com.edu.bean.User;
import com.edu.service.UserNewsService;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user/news/detail")
public class UserNewsDetailServlet extends HttpServlet { private UserNewsService userNewsService = new UserNewsService();
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idStr = request.getParameter("id");
        if (idStr != null && !idStr.trim().isEmpty()) {
            try {
                int newsId = Integer.parseInt(idStr);
                News news = userNewsService.userGetNewsDetail(newsId);
                if (news != null && news.getStatus() == 1) {  // 确保新闻存在且为上架状态
                    request.setAttribute("news", news);
                    request.setAttribute("comments", userNewsService.userGetNewsComments(newsId));
                    request.getRequestDispatcher("/user/news/detail.jsp").forward(request, response);
                    return;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        response.sendRedirect(request.getContextPath() + "/user/home");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        Map<String, Object> result = new HashMap<>();

        // 检查用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            objectMapper.writeValue(response.getWriter(), result);
            return;
        }

        String newsIdStr = request.getParameter("newsId");
        String content = request.getParameter("content");

        if (newsIdStr != null && !newsIdStr.trim().isEmpty() &&
                content != null && !content.trim().isEmpty()) {
            try {
                int newsId = Integer.parseInt(newsIdStr);
                // 检查新闻是否存在且为上架状态
                News news = userNewsService.userGetNewsDetail(newsId);
                if (news == null || news.getStatus() != 1) {
                    result.put("success", false);
                    result.put("message", "该新闻不存在或已下架");
                    objectMapper.writeValue(response.getWriter(), result);
                    return;
                }

                boolean success = userNewsService.userAddComment(newsId, user.getUserId(), content.trim());
                result.put("success", success);
                if (!success) {
                    result.put("message", "评论失败");
                }
            } catch (NumberFormatException e) {
                result.put("success", false);
                result.put("message", "参数错误");
            }
        } else {
            result.put("success", false);
            result.put("message", "参数错误");
        }

        objectMapper.writeValue(response.getWriter(), result);
    }

} 